import pandas as pd
import numpy as np
import csv

# 低方差滤波器 数据降维
from sklearn.decomposition import PCA

import random
seed = 1
random.seed(seed)
np.random.seed(seed)

def data_reduction(path_csv):
    # read the data
    train = pd.read_csv(path_csv, header=None)
    pca = PCA(n_components=8)
    # train_data=train.iloc[:,0:44].values
    pca_result = pca.fit_transform(train.values)
    no = []
    for i in range(100000):
        no.append(i)
    pca_result = np.insert(pca_result, 0, values=no, axis=1)
    pca_result = np.concatenate((pca_result, train), axis=1)
    write_result(pca_result)
    return pca_result.tolist()

def write_result(result):
    f = open('data_reduction_result.csv', 'w', encoding='utf-8', newline='')
    csv_writer = csv.writer(f)
    for res in result:
        csv_writer.writerow(res)
    f.close()

input_example = 'data.csv'
output_example = 'data_reduction_example.csv'
output = data_reduction(input_example)

def read_csv(csv_name):
    f = open(csv_name, 'r')
    reader = csv.reader(f)
    result = []
    for row in reader:
        row = [float(s) for s in row]
        result.append(row)
    f.close()
    return result

def compare(o_e, o):
    result_example = read_csv(o_e)
    result = o
    if (result_example == result):
        return True
    else:
        return False

print("")
# 用例目的
print("该用例目的为：读取待处理数据并进行数据降维操作")
print("")
# 子用例编号
print("子用例编号：data_reduction_1")
print("")
print("****************************")
print("当前输入为：")
print(input_example)

print("")

print("****************************")
print("当前输出格式为:")
print([len(output),len(output[0])])

print("****************************")
print("由于数据量过大，展示输出中的前五行:")
print(output[:5])
print("")
print("****************************")
print("预设输出格式为:")
print([len(read_csv(output_example)),len(read_csv(output_example)[0])])

print("****************************")
print("由于数据量过大，展示预设输出中的前五行:")
# 输出处理后数据
print(read_csv(output_example)[:5])
print("")

print("****************************")
print("是否正确:")
# 输出对比结果
# 需要写一个compare函数
if compare(output_example, output):
    print("输出与预定目标相符")
else:
    print("输出与预定目标不符")
# 输出对比结果

print("\n")
